// 自定义响应对象，与 express 中 Response 做区分
class Resp {
    resp = {};

    public setData(data: Object): Resp {
        this.resp['data'] = data;
        return this;
    }

    // 请求成功标准模版
    public setSuccess(): Resp {
        this.resp['code'] = 0;
        this.resp['msg'] = 'success';
        this.resp['data'] = {};
        return this;
    }

    // 请求失败标准模版
    public setFail(): Resp {
        this.resp['code'] = 500;
        this.resp['msg'] = 'error';
        this.resp['data'] = {};
        return this;
    }

    public setCode(code: number): Resp {
        this.resp['code'] = code;
        return this;
    }

    public setMsg(msg: string): Resp {
        this.resp['msg'] = msg;
        return this;
    }

    // 保证 resp 对象只有一个
    public collect(): Object {
        return this.resp;
    }
}

export const resp = new Resp();
